package com.yonyou.pmclouds.projecttype.process;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.projecttype.entity.ProjectTypeVO;
import com.yonyou.pmclouds.projecttype.mapper.ProjectTypeMapper;

public class ProjectTypeUpdateCheckProcessor implements SingleProcessor<ProjectTypeVO> {
    private ProjectTypeMapper mapper;
    public ProjectTypeUpdateCheckProcessor(ProjectTypeMapper mapper){
        this.mapper=mapper;
    }
    @Override
    public void process(ProjectTypeVO vo, ProjectTypeVO orig) throws BusinessException {
        ProjectTypeVO enable = mapper.isEnable(vo.getPkProjectType(), vo.getPkTenant());
        if (enable.getPkParent()==null){
//确定为一级类别，则查询一级类别下的所有对象
            ProjectTypeVO[] projectTypeVO = mapper.queryFirstCategory(vo.getPkTenant());
            if (projectTypeVO!=null){
                //待优化
                for (ProjectTypeVO projectTypeVO1:projectTypeVO){
                    if (projectTypeVO1.getPkProjectType().equals(vo.getPkProjectType())){
                        continue;
                    }
                    if (vo.getSname().equals(projectTypeVO1.getSname())&&vo.getScode().equals(projectTypeVO1.getScode())){
                        throw new BusinessException("编号和名称都已存在");
                    }else if (vo.getSname().equals(projectTypeVO1.getSname())){
                        throw new BusinessException("名称已存在");
                    }else if (vo.getScode().equals(projectTypeVO1.getScode())){
                        throw new BusinessException("编号已存在");
                    }
                }
        }
    }else {
            ProjectTypeVO[] projectTypeVO = mapper.queryRepetition(enable.getPkParent(), enable.getPkRoot(),vo.getPkTenant());
            if (projectTypeVO!=null){
                for (ProjectTypeVO projectTypeVO1:projectTypeVO){
                    if (projectTypeVO1.getPkProjectType().equals(vo.getPkProjectType())){
                        continue;
                    }
                    if (vo.getSname().equals(projectTypeVO1.getSname())&&vo.getScode().equals(projectTypeVO1.getScode())){
                        throw new BusinessException("编号和名称都已存在");
                    }else if (vo.getSname().equals(projectTypeVO1.getSname())){
                        throw new BusinessException("名称已存在");
                    }else if (vo.getScode().equals(projectTypeVO1.getScode())){
                        throw new BusinessException("编号已存在");
                    }
                }
            }
        }
    }
}
